using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Winter.BackEnd.Models.Domain;
using System.Web.UI.MobileControls;

namespace Winter.BackEnd.Controllers
{
    [Authorize]
    public class UniversitiesController : ApplicationController
    {

        [AcceptVerbs(HttpVerbs.Get), Authorize(Roles = "admin")]
        public ActionResult Index()
        {
            return View(Repository.CurrentDataContext.Universities.FirstOrDefault());
        }


        [AcceptVerbs(HttpVerbs.Get), Authorize(Roles = "admin")]
        public ActionResult Details(int id)
        {
            return View();///TODO: review
        }


        [AcceptVerbs(HttpVerbs.Get), Authorize(Roles = "admin")]
        public ActionResult Create()
        {
            return View();
        }


        [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "admin")]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


        [AcceptVerbs(HttpVerbs.Get), Authorize(Roles = "admin")]
        public ActionResult Edit(int id)
        {
            return View();
        }


        [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "admin")]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        #region HelperMethods
       
        /// TODO: need refactoring
      
        public JsonResult GetItemDetails(string CategoryName, int Id)
        {
            var result = new JsonResult();
            if (CategoryName.Contains("faculty"))
            {
                var faculty = Repository.CurrentDataContext.Faculties.SingleOrDefault(u => u.ID == Id);
                result.Data = new
                {
                    Name = faculty.Name,
                    StudentsNumber = Repository.CurrentDataContext.Students.Count(st => faculty.Departments.Contains(st.Group.Department))
                };

            }
            else if (CategoryName.Contains("department"))
            {
                var department = Repository.CurrentDataContext.Departments.SingleOrDefault(u => u.ID == Id);
                result.Data = new
                {
                    Name = department.Name,
                    StudentsNumber = Repository.CurrentDataContext.Students.Count(st => department.Groups.Contains(st.Group))
                };
            }
            else if (CategoryName.Contains("group"))
            {
                var Group = Repository.CurrentDataContext.Groups.SingleOrDefault(u => u.ID == Id);
                result.Data = new
                {
                    Name = Group.Name,
                    StudentsNumber = Group.Students.Count()
                };
            }
            return result;
        }
        public JsonResult GetStudetnsForItem(string CategoryName, int Id)
        {
            var result = new JsonResult();
            var page = new { page = 1 };
            if (CategoryName.Contains("faculty"))
            {
                var faculty = Repository.CurrentDataContext.Faculties.SingleOrDefault(u => u.ID == Id);


                var rows = (from st in Repository.CurrentDataContext.Students.Where(st => faculty.Departments.Contains(st.Group.Department))
                            select new
                            {
                                id = st.ID,
                                cell = new[] { 
                                st.ID.ToString(), 
                                st.Group.Department.Faculty.Name,
                                st.Group.Department.Name,
                                st.Group.Name, 
                                st.Person.FullName 
                            }
                            }).ToList();
                result.Data = new { page = 1, records = rows.Count(), rows, total = 1 };
            }
            else if (CategoryName.Contains("department"))
            {
                var department = Repository.CurrentDataContext.Departments.SingleOrDefault(u => u.ID == Id);

                var rows = (from st in Repository.CurrentDataContext.Students.Where(st => department.Groups.Contains(st.Group))
                            select new
                            {
                                id = st.ID,
                                cell = new[] { 
                                st.ID.ToString(), 
                                st.Group.Department.Faculty.Name,
                                st.Group.Department.Name,
                                st.Group.Name, 
                                st.Person.FullName 
                            }
                            }).ToList();
                result.Data = new { page = 1, records = rows.Count(), rows, total = 1 };
            }
            else if (CategoryName.Contains("group"))
            {
                var Group = Repository.CurrentDataContext.Groups.SingleOrDefault(u => u.ID == Id);

                var rows = (from st in Group.Students
                            select new
                            {
                                id = st.ID,
                                cell = new[] { 
                                st.ID.ToString(), 
                                st.Group.Department.Faculty.Name,
                                st.Group.Department.Name,
                                st.Group.Name, 
                                st.Person.FullName 
                            }
                            }).ToList();
                result.Data = new { page = 1, records = rows.Count(), rows, total = 1 };
            }
            return result;
        }
        #endregion
    }
}
